<template>
  <div class="">
    <div class="pic-index-list flex-center">
      <div
        class="btn btn-left hover"
        @click="onScroll('left')"
        :class="currentIndex === 0 && 'btn-disable'"
      ></div>
      <div class="index-list flex" ref="indexListRef">
        <div
          class="index-item flex-center hover"
          :class="currentIndex === index && 'index-item-active'"
          v-for="(_, index) in imgUrl"
          :key="index"
          @click="currentIndex = index"
        >
          {{ index + 1 }}
        </div>
      </div>
      <div
        class="btn btn-right hover"
        :class="
          (imgUrl.length === 1 || currentIndex === imgUrl.length - 1) &&
          'btn-disable'
        "
        @click="onScroll('right')"
      ></div>
    </div>
    <div class="flex-center">
      <img
        :src="imgUrl[currentIndex]"
        @click="onOpenPreview"
        class="pic-img hover"
        draggable="false"
      />
    </div>
  </div>
  <ImagePreview
    v-if="showImagePreview"
    v-model:isShow="showImagePreview"
    :currentIndex="currentIndex"
    :imgList="imgUrl"
    :studentName="studentName"
    :questionIndex="questionIndex"
  />
</template>

<script setup>
import { ref, defineModel } from 'vue'
import ImagePreview from './ImagePreview.vue'
const props = defineProps({
  questionIndex: {
    required: false
  },
  studentName: {
    required: false
  },
  imgUrl: {
    type: Array,
    required: true
  }
})

const indexListRef = ref()
const currentIndex = defineModel('imgIndex', { default: 0, required: false })
const showImagePreview = ref(false)

const onScroll = type => {
  if (type === 'left') {
    currentIndex.value = currentIndex.value === 0 ? 0 : currentIndex.value - 1
    indexListRef.value.scrollLeft -= 30
    return
  }
  currentIndex.value =
    currentIndex.value === props.imgUrl.length - 1
      ? currentIndex.value
      : currentIndex.value + 1
  indexListRef.value.scrollLeft += 30
}
const onOpenPreview = () => {
  showImagePreview.value = true
}
</script>

<style lang="less" scoped>
.pic-index-list {
  gap: 20px;
  margin-bottom: 20px;
  .btn {
    width: 30px;
    height: 30px;
    margin-bottom: -4px;
  }
  .btn-disable:hover {
    cursor: not-allowed;
  }
  .btn-left {
    background-image: url('@/assets/images/assignment/pic-left.png');
    background-repeat: no-repeat;
    background-size: contain;
  }
  .btn-right {
    background-image: url('@/assets/images/assignment/pic-right.png');
    background-size: contain;
    background-repeat: no-repeat;
  }
  .index-list {
    max-width: 80%;
    overflow-x: auto;
    &::-webkit-scrollbar {
      width: 0px;
      height: 0;
    }

    gap: 20px;
    .index-item {
      color: #cdced1;
      border: 1px solid #cdced1;
      border-radius: 100%;
      width: 30px;
      height: 30px;
      flex-shrink: 0;
    }
    .index-item-active {
      color: #428fff;
      border-color: #428fff;
    }
  }
}
.pic-img {
  width: 70%;
}
</style>
